import {computed, reactive, ref} from "vue";
import {FetchCourseZSListDAT} from "../../types/responseTypes";
import {useStore} from "vuex";
import {getCourseZSList,getRDCourseList} from "../../api/classes";

export function useCourseZSList(){

    const total = ref(0)
		const COUNT = ref(0)
    const courseZSListWrap = reactive<{
        courseZSList: FetchCourseZSListDAT[]
    }>({
        courseZSList: []
    })
		const ZyClass = ref<any>([])
		const ZyClassList = ref<any>([])
    const {state} = useStore()
    function initCourseZSList(page: number = 1, pageSize: number = 20,params?: Partial<{
        COURSE_ID: string
        CLASSNAME: string
        STATE: ""
    }>){
        return new Promise((resolve, reject) => {
            getCourseZSList({
                COURSE_ID: String(params?.COURSE_ID),
                MAJOR_ID: '',
                CLASS_ID: '',
                CLASSNAME: params?.CLASSNAME || '',
                STATE: "",
                PLACE: '',
                PAGEINDEX: page,
                PAGESIZE: pageSize,
                OPERATOR_ID: state.user.userInfo.CUSER_ID
            }).then(res => {
                if (res.ERROR[0].errcode == '0'){
                    courseZSListWrap.courseZSList = JSON.parse(JSON.stringify(res.DAT))
                    total.value = parseInt(String(res.COUNT[0].NUM / 20)) + (res.COUNT[0].NUM % 20 === 0 ? 0 : 1)
                    resolve(null)
                }else {
                    uni.showToast({
                        title: res.ERROR[0].error,
                        icon: 'none',
                        mask: true
                    })
                    reject('err')
                }
            }).catch(err => {
                uni.showToast({
                    title: '获取列表失败',
                    icon: 'error',
                    mask: true
                })
                reject('err')
            })
        })
    }
	function initgetRDCourseList(MAJOR_ID:string,PAGEINDEX:number){
		return new Promise((resolve,reject)=>{
			getRDCourseList({
				COURSE_ID:"",
				MAJOR_ID:MAJOR_ID?MAJOR_ID:"",
				COURSENO:"",
				COURSENAME:"",
				TEACHER_NAME:"",
				STATE:"" ,
				PAGEINDEX:PAGEINDEX?PAGEINDEX:1,
				PAGESIZE:20,
				OPERATOR_ID:state.user.userInfo.CUSER_ID,
				
			})
			.then(res=>{
				if (res.ERROR[0].errcode == '0'){
					ZyClass.value = res.DAT
					COUNT.value = parseInt(String(res.COUNT[0].NUM / 20)) + (res.COUNT[0].NUM % 20 === 0 ? 0 : 1)
					resolve(null)
					ZyClassList.value = []
					ZyClassList.value.push(...res.DAT.map(item => ({
					    value: String(item.COURSE_ID),
					    label: item.COURSENAME,
							CLASSLEADER:item.CLASSLEADER,
							COURSENO:item.COURSENO,
							STATE:item.STATE,
							STATENR:item.STATENR,
							TEACHER_NAME:item.TEACHER_NAME
					})))
				}else {
					uni.showToast({
							title: res.ERROR[0].error,
							icon: 'none',
							mask: true
					})
					reject('err')
				}
			}).catch(err => {
				console.log('getRDCourseList err: ', err)
				uni.showToast({
						title: '获取列表失败',
						icon: 'error',
						mask: true
				})
				reject('err')
			})
		})
	}
    return {
        total,
        courseZSList: computed(() => courseZSListWrap.courseZSList),
        initCourseZSList,
				initgetRDCourseList,
				ZyClass,
				COUNT,
				ZyClassList
    }
}
